package org.i1510.common.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.i1510.common.bean.PageBean;
import org.i1510.common.dao.ISysPopDao;
import org.i1510.common.po.SysPop;
import org.i1510.common.service.ISysPopService;
import org.i1510.common.utils.BeanUtils;
import org.i1510.common.utils.DateUtils;
import org.i1510.common.utils.IDGenerator;
import org.i1510.common.utils.UserUtils;
import org.i1510.common.utils.web.ResultMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("iSysPopService")
public class SysPopServiceImpl extends BaseServiceImpl implements ISysPopService{
	@Autowired
	private ISysPopDao iSysPopDao;	
	
	public ResultMapper save(SysPop basePo,HttpServletRequest request) {

		ResultMapper mapper = ResultMapper.generateFailure();
		SysPop saved = null;
		if(null !=basePo && basePo.getDocId() != null && !basePo.getDocId().equals("")){//修改
			SysPop finder = iSysPopDao.findById(basePo.getDocId());   
			if(null != finder){
				finder.setDocLastTime(DateUtils.getNow());
				finder.setDocLastor(UserUtils.getSessionUserBean(request).getSysUser().getDocId());			
			    BeanUtils.copy(finder, basePo);
			    saved = iSysPopDao.save(finder);
			}else{
				mapper = ResultMapper.generateFailure();
				return mapper;
			}
			
		}else{//新增
			basePo.setDocId(IDGenerator.getGenerateID());
			basePo.setDocCreateTime(DateUtils.getNow());
			basePo.setDocCreator(UserUtils.getSessionUserBean(request).getSysUser().getDocId());
			saved = iSysPopDao.save(basePo);
		}

		if (saved == null) {
			mapper = ResultMapper.generateFailure();
			return mapper;
		}else{
			mapper = ResultMapper.generateSuccess();
			return mapper;			
		}
	}
	public ResultMapper delete(String docId) {

		ResultMapper mapper = ResultMapper.generateFailure();
		iSysPopDao.delete(docId);
		mapper = ResultMapper.generateSuccess();
		return mapper;
	}

	
	public SysPop findById(String docId){
		return  iSysPopDao.findById(docId);
	}
	
	public List<SysPop> findByIds(List<String> docIds){
		return (List<SysPop>) iSysPopDao.findAll(docIds);
	}
	public List getPage(SysPop sysPop,PageBean pageBean,Map conditions) {
		Map map = new HashMap();
		//Bean转成Map
		BeanUtils.ObjectCopyMap(sysPop, map);
		map.put("intPageIndex", pageBean.getIntPageIndex());
		map.put("intPageSize", pageBean.getIntPageSize());
		map.putAll(conditions);
		List list = this.queryForListBySql("sysPopSql.getPage", map);
		return list;
	}
	public int getPageCount(SysPop sysPop,Map conditions) {
		Map map = new HashMap();
		//Bean转成Map
		BeanUtils.ObjectCopyMap(sysPop, map);
		map.putAll(conditions);
		List list = this.queryForListBySql("sysPopSql.getPageCount", map);		
		return list.size();
	}

	@Transactional
	public ResultMapper saveAll(List<SysPop> list) {

		ResultMapper mapper = ResultMapper.generateFailure();

		List<SysPop> listSaved = iSysPopDao.save(list);
		
		if (listSaved == null) {
			mapper = ResultMapper.generateFailure();
			return mapper;
		}
		mapper = ResultMapper.generateSuccess();
		return mapper;
	}
	public List getSysUser4Pop(Map conditions){
		Map map = new HashMap();
		map.putAll(conditions);
		List list = this.queryForListBySql("sysPopSql.getSysUser4Pop", map);
		return list;
	}
}